package com.example.loginproject.view.fragments

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import com.example.loginproject.databinding.FragmentPinLoginBinding
import com.example.loginproject.tool.AnimTools
import com.example.loginproject.tool.MyLog
import com.example.loginproject.tool.navigateTo
import com.example.loginproject.user.PasswordType
import com.example.loginproject.user.UserManager
import com.example.loginproject.view.activity.RootActivity
import com.example.loginproject.view.viewModels.ButtonStata
import com.example.loginproject.view.viewModels.LoginStata
import com.example.loginproject.view.viewModels.SharedViewModel


class PinLoginFragment : Fragment() {
    private lateinit var binding:FragmentPinLoginBinding
    private val viewModel: SharedViewModel by activityViewModels()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPinLoginBinding.inflate(inflater,null,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //设置 切换到图案解锁的显示和隐藏
        /*
        if (UserManager.sharedInstance(requireContext()).hasUser()){
            binding.switchToPatternTextView.visibility = View.VISIBLE
        }else{
            binding.switchToPatternTextView.visibility = View.INVISIBLE
            binding.button.isEnabled = false //没有用户 无法点击登录按钮
        }
         */
        //监听是否需要显示的切换到图案解锁的视图
        viewModel.showChange.observe(viewLifecycleOwner){
            binding.switchToPatternTextView.visibility = if(it) View.VISIBLE else View.INVISIBLE
        }
        viewModel.loginBtnIsEnable.observe(viewLifecycleOwner){
            binding.button.isEnabled = it
        }
        binding.switchToPatternTextView.setOnClickListener {
            navigateTo(PatternLoginFragment())
        }
        //监听用户输入框改变事件
        binding.nameInputView.textChangedListener ={
            userInputView, text ->
            if (text.isNotEmpty()&&binding.nameInputView.text.isNotEmpty()){
              viewModel.changeButtonStata(ButtonStata.Enabled)
            }else{
                viewModel.changeButtonStata(ButtonStata.UnEnabled)
            }
        }
        binding.passwordInputView.textChangedListener = { userInputView, text ->
            if (text.isNotEmpty() && binding.passwordInputView.text.isNotEmpty()) {
                viewModel.changeButtonStata(ButtonStata.Enabled)
            } else {
                viewModel.changeButtonStata(ButtonStata.UnEnabled)
            }
        }
        viewModel.loginStata.observe(viewLifecycleOwner){
            when(it){
                LoginStata.Success->{
                   val intent = Intent(requireActivity(),RootActivity()::class.java)
                    startActivity(intent)
                }
                LoginStata.Failure->{
                    binding.nameInputView.showError()
                    binding.passwordInputView.showError()
                    AnimTools.startSwingAnim(binding.button)
                }
                LoginStata.Default->{}
            }
            Toast.makeText(context,"$it",Toast.LENGTH_LONG).show()
        }
        binding.button.setOnClickListener {
            viewModel.login(binding.nameInputView.text,binding.passwordInputView.text,PasswordType.Pin)
        }
        binding.registerTextView.setOnClickListener {
            navigateTo(PinRegisterFragment())
        }

    }
    override fun onResume() {
        super.onResume()
    }
}